package com.my.screenadapter.net;

import android.util.Log;
import android.view.View;

import com.ihsanbal.logging.Level;
import com.ihsanbal.logging.LoggingInterceptor;
import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.BuildConfig;
import com.my.screenadapter.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.platform.Platform;

/**
 * okhttp 网络框架总结
 *
 * 1 简单使用
 *
 * 接口他推荐 ：https://www.zhihu.com/question/39479153
 *
 *
 */
public class OkHttpActivity extends BaseActivity {

    private static final String TAG = "OkHttpActivity";
    private static final String baseUrl = "https://api.github.com";
    private OkHttpClient client;
    private Call baseurlCall;

    public void startOkHttp(View view) {
            okhttp3.OkHttpClient.Builder httpClientBuilder = new okhttp3.OkHttpClient.Builder();
            //add log record
            if (BuildConfig.DEBUG) {
                //打印网络请求日志
                LoggingInterceptor httpLoggingInterceptor = new LoggingInterceptor.Builder()
                        .loggable(BuildConfig.DEBUG)
                        .setLevel(Level.BASIC)
                        .log(Platform.INFO)
                        .request("Request")
                        .response("Response")
                        .build();
                httpClientBuilder.addInterceptor(httpLoggingInterceptor);
            }

        Request request = new Request.Builder().url(baseUrl).build();

        client = httpClientBuilder.build();
        baseurlCall = client.newCall(request);
        baseurlCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: ",e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i(TAG, "onResponse: "+response.body().string());
            }
        });

        // timeout -->header
//            int DEFAULT_TIMEOUT = 60;
//            httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//                    .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//                    .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//                    .addInterceptor(new Interceptor() {
//                        @Override
//                        public Response intercept(Chain chain) throws IOException {
//                            Request originalRequest = chain.request();
//                            Request.Builder requestBuilder = originalRequest.newBuilder()
//                                    .header("Content-Type", "application/json;charset=UTF-8")
//                                    .header("Accept", "application/json")
//                                    .header("Accept-Encoding", "gzip")
//                                    .method(originalRequest.method(), originalRequest.body());
//                            Request request = requestBuilder.build();
//                            return chain.proceed(request);
//                        }
//                    });
    }

    public void cancleOkHttp(View view) {
        if (baseurlCall != null) {

            baseurlCall.cancel();
        }
    }

    @Override
    protected void init() {
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_ok_http;
    }

//    OkHttpClient client = new OkHttpClient();

    String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}
